package cn.itcast.bos.web.action.system;

import java.io.IOException;
import java.util.List;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.serializer.SerializerFeature;

import cn.itcast.bos.domain.system.Role;
import cn.itcast.bos.service.system.RoleService;
import cn.itcast.bos.web.action.common.BaseAction;

@Namespace("/")
@ParentPackage("struts-default")
@Scope("prototype")
@Controller
public class RoleAction extends BaseAction<Role> {
	private static final long serialVersionUID = 1L;
	@Autowired
	private RoleService roleService;

	// 查询
	@Action("role_list")
	public void list() throws IOException {

		List<Role> roles = roleService.findAll();
		String jsonString = JSONArray.toJSONString(roles, SerializerFeature.DisableCircularReferenceDetect);
		ServletActionContext.getResponse().setContentType("application/json;charset=utf-8");
		ServletActionContext.getResponse().getWriter().write(jsonString);

	}

	// 添加
	private String menuIds;
	private String[] permissionIds;

	public void setMenuIds(String menuIds) {
		this.menuIds = menuIds;
	}

	public void setPermissionIds(String[] permissionIds) {
		this.permissionIds = permissionIds;
	}

	@Action(value = "role_save", results = {
			@Result(name = "success", location = "./pages/system/role.html", type = "redirect") })
	public String save() {
		roleService.save(model,menuIds,permissionIds);
		return SUCCESS;
	}
}
